import asyncio
import aiomysql.sa as aio_sa


class Mysql_data:

    async def msql_data(self, insert_sql, sql_data=None):
        async with aio_sa.create_engine(host="localhost",  # 
                                        port=3306,
                                        user="root",
                                        password="",
                                        db="") as engine:

            async with engine.acquire() as conn:
                # 注意: 执行的执行必须开启一个事务, 否则数据是不会进入到数据库中的
                async with conn.begin():
                    if sql_data == None:
                        result = await conn.execute(insert_sql)
                        data = await result.fetchall()
                        return data
                    else:
                        await conn.execute(insert_sql, sql_data)



    def run_sql(self, tada, dele=None):
        self.loop1 = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop1)
        self.loop = asyncio.get_event_loop()
        game_id = self.loop.run_until_complete(self.msql_data(tada, dele))

        return game_id

    async def msql_data_1(self, insert_sql, sql_data=None):
        async with aio_sa.create_engine(host="",  
                                        port=33306,
                                        user="",
                                        password="",
                                       db="") as engine:
            async with engine.acquire() as conn:
                # 注意: 执行的执行必须开启一个事务, 否则数据是不会进入到数据库中的
                async with conn.begin():
                    if sql_data == None:
                        result = await conn.execute(insert_sql)
                        data = await result.fetchall()
                        return data
                    else:
                        await conn.execute(insert_sql, sql_data)

    def run_sql_1(self, tada, dele=None):
        self.loop1 = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop1)
        self.loop = asyncio.get_event_loop()
        game_id = self.loop.run_until_complete(self.msql_data_1(tada, dele))
        return game_id

    async def msql_data_2(self, insert_sql, sql_data=None):
        async with aio_sa.create_engine(host="",  # 
                                        port=3306,
                                        user="",
                                        password="",
                                       db="") as engine:
            async with engine.acquire() as conn:
                # 注意: 执行的执行必须开启一个事务, 否则数据是不会进入到数据库中的
                async with conn.begin():
                    if sql_data == None:
                        result = await conn.execute(insert_sql)
                        data = await result.fetchall()
                        return data
                    else:
                        await conn.execute(insert_sql, sql_data)

    def run_sql_2(self, tada, dele=None):
        self.loop1 = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop1)
        self.loop = asyncio.get_event_loop()
        game_id = self.loop.run_until_complete(self.msql_data_2(tada, dele))
        return game_id

    def loop_colos(self):
        self.loop.close()
